package com.web.new12306.controller;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.web.new12306.conn.Md5Utils;
import com.web.new12306.pojo.User;
import com.web.new12306.service.UserService;
import com.web.new12306.service.impl.UserServiceImpl;



/**
 * 
 * @author 苗宇
 *
 */

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String action = request.getParameter("action");

		if ("login".equals(action)) {
			doLogin(request, response);
		} else if ("autoLogin".equals(action)) {
			doAutoLogin(request, response);
		}
	}
     //自动登录
	private void doAutoLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	private void doLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		 Cookie username = new Cookie("name", URLEncoder.encode(request.getParameter("username"), "UTF-8")); // 中文转码
	     Cookie password= new Cookie("password",URLEncoder.encode(request.getParameter("password"), "UTF-8"));
	        
	        // 为两个 Cookie 设置过期日期为 24 小时后
	        username.setMaxAge(60*60*24); 
	        password.setMaxAge(60*60*24); 
	        
	        // 在响应头中添加两个 Cookie
	        response.addCookie(username);
	        response.addCookie(password );
	       
	    HttpSession session = request.getSession();
		User user = new User();
		String name=request.getParameter("username");
		session.setAttribute("username", name);
		String pwd=request.getParameter("password");
		String code=request.getParameter("code");
		user.setUsername(name);
		user.setPassword(pwd);
		user.setCode(code);

		
		String msg=validate(user);  
		if (msg==null) {
			String code1=(String) session.getAttribute("code");
			if (!user.getCode().equalsIgnoreCase(code1)) {
				msg = "验证码错误";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("/userLogin.jsp").forward(request,
						response);
			} else {
				// 检查用户是否存在
				UserService userService=new UserServiceImpl();
				boolean flag=userService.Login(name,Md5Utils.md5(pwd));// Md5Utils.md5(pwd)报错
				System.out.println(flag);
				if (flag) {
					request.getSession().setAttribute("user", user);
//					request.getRequestDispatcher("/showAllServlet").forward(request, response);//showAllServlet
					request.getRequestDispatcher("/frontstage.html").forward(request,
							response);
					
				} 
				else {
					msg = "用户名或密码错误";
					request.setAttribute("msg", msg);
					request.getRequestDispatcher("/userLogin.jsp").forward(request,
							response);
				}
			}
		} else {
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("/userLogin.jsp").forward(request,
					response);
		}
	}
	
	private String validate(User user) {
		String errorMsg = null;
		if (user.getUsername()=="") {
			errorMsg = "请输入用户名";
		} 
		else if (user.getUsername().length() < 6
				|| user.getUsername().length() > 30) {
			errorMsg = "用户名长度在6到30位之间";
		}
		else if (user.getPassword()=="") {
			errorMsg = "请输入密码";
		} else if (user.getCode()=="") {
			errorMsg = "请输入验证码";
		}
		return errorMsg;
	}
	
}
